<?php
namespace data\admin;
use \data\BaseData;
/**
 * 管理员数据管理
 */
class AdminData extends BaseData{
    
    protected $table = 'admin';
    /**
     * 读取一个管理员信息
     */
    public function find($param){
        $check = ['adminid','account','mobile'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $fields = 'a.*,r.type,r.role,r.id as roleid';
        $info  = M('admin')->alias('a')->join('elite_sys_power_role r','a.roleid = r.id')->field( $fields)
        ->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个管理员信息
     */
    public function add($data){        
        $check = [
            'agentid'   => ['check'=>'must','msg'=> '代理商ID不能为空','reg'=>'int'],
            'status'    => ['def'=>1],
            'account'   => ['check'=>'must','msg'=>'账号不能为空','reg'=>'account'],
            'password'  => ['check'=>'must','msg'=>'密码不能为空'],
            'passsalt'  => ['check'=>'must'],
            'name'      => '',
            'mobile'    => ['reg'=>'mobile'],
            'roleid'    => ['check'=>'must','msg'=> '角色不能为空','reg'=>'int'],
            'addtime'   => ['val'=> SYS_TIME],
            'edittime'  => ['val'=> SYS_TIME],  
        ];
        $save = $this->verifyData($check,$data);
        return M('admin')->insertGetId($save);
    }
    
    /**
     * 修改一个管理员信息
     */
    public function edit($adminid, $data){
        $check = [
            'agentid'   => ['reg'=>'int'],
            'status'    => ['reg'=>[1,2]],
            'type'      => ['reg'=>[1,2,3]],
            'account'   => ['reg'=>'account'],
            'password'  => '',
            'passsalt'  => '',
            'name'      => '',
            'mobile'    => ['reg'=>'mobile'],
            'roleid'    => ['reg'=>'int'],           
            'edittime'  => ['val'=> SYS_TIME],  
        ];
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['adminid'=>$adminid];
        $result = M('admin')->where($where)->update($save);
        if($result === false ) dataE('修改账户信息失败');
        return $result;
    }
    
}